<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { RouterView, useRouter } from 'vue-router'
import { createStarfield } from './utils/uiUtils'
import { getStorage, removeStorage } from './utils/storage'

const router = useRouter()
const currentUser = ref(getStorage('username'))
const isLoggedIn = ref(getStorage('isLoggedIn') === 'true')

// 定时器和清理函数引用
let cleanupMeteors = null
let starfieldInterval = null

// 检查登录状态
const checkLoginStatus = () => {
  const loggedIn = getStorage('isLoggedIn') === 'true'
  isLoggedIn.value = loggedIn
  
  if (loggedIn) {
    currentUser.value = getStorage('username') || ''
  }
}

// 处理登出
const handleLogout = () => {
  removeStorage('isLoggedIn')
  removeStorage('username')
  isLoggedIn.value = false
  currentUser.value = ''
  router.push('/login')
}

onMounted(() => {
  checkLoginStatus()
  
  // 使用工具函数创建星空背景
  cleanupMeteors = createStarfield('star-background')
  
  // 每30秒重新生成星星
  starfieldInterval = setInterval(() => {
    if (cleanupMeteors) {
      cleanupMeteors()
    }
    cleanupMeteors = createStarfield('star-background')
  }, 30000)
})

// 组件卸载时清理资源
onUnmounted(() => {
  if (cleanupMeteors) {
    cleanupMeteors()
  }
  if (starfieldInterval) {
    clearInterval(starfieldInterval)
  }
})
</script>

<template>
  <div id="app">
    <!-- 星空背景组件 -->
    <div class="star-background">
      <div class="gradient-animation"></div>
      <!-- 星星和流星会通过JavaScript动态生成 -->
    </div>
    
    <header>
      <nav class="navbar">
        <div class="container">
          <!-- 导航栏 - 全部横向排列 -->
          <div class="nav-container">
            <!-- 左侧标题 -->
            <router-link to="/" class="nav-item title-item">游戏乐园</router-link>
            
            <!-- 中间导航链接 -->
            <router-link to="/games" class="nav-item">游戏中心</router-link>
            <router-link to="/profile" class="nav-item">个人中心</router-link>
            
            <!-- 右侧功能区域 -->
            <div class="nav-spacer"></div> <!-- 占位，确保右侧元素靠右对齐 -->
            
            <!-- VIP链接 -->
            <router-link to="/vip-recharge" class="nav-item vip-item">VIP充值</router-link>
            
            <!-- 用户信息（登录状态） -->
            <div class="nav-item user-info" v-if="isLoggedIn">
              <span class="welcome-text">欢迎，{{ currentUser }}</span>
              <button class="logout-button" @click="handleLogout">登出</button>
            </div>
            
            <!-- 登录注册按钮（未登录状态） -->
            <div class="nav-item auth-buttons" v-else>
              <router-link to="/login" class="auth-button">登录</router-link>
              <router-link to="/register" class="auth-button">注册</router-link>
            </div>
          </div>
        </div>
      </nav>

      <!-- 主内容区域 -->
      <main class="main-content">
        <RouterView />
      </main>
    </header>
  </div>
</template>

<style scoped>
/* 全局重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* 主应用容器 */
#app {
  position: relative;
  width: 100%;
  min-height: 100vh;
}

/* 确保背景在底层 */
  .star-background {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    overflow: hidden;
    background: linear-gradient(135deg, #0f172a 0%, #1e293b 50%, #0f172a 100%);
  }
  
  /* 渐变动画效果 */
  .gradient-animation {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(circle at 20% 50%, rgba(102, 126, 234, 0.1) 0%, transparent 50%),
                radial-gradient(circle at 80% 30%, rgba(118, 75, 162, 0.1) 0%, transparent 50%),
                radial-gradient(circle at 40% 80%, rgba(255, 107, 107, 0.1) 0%, transparent 50%);
    animation: gradientShift 20s ease-in-out infinite;
  }
  
  /* 星星样式 */
  .star {
    position: absolute;
    background-color: white;
    border-radius: 50%;
    animation: twinkle 3s ease-in-out infinite;
  }
  
  /* 流星样式 */
  .shooting-star {
    position: absolute;
    width: 2px;
    height: 2px;
    background-color: white;
    border-radius: 50%;
    animation: shootingStar 10s linear infinite;
    box-shadow: 0 0 6px 2px rgba(255, 255, 255, 0.8);
  }
  
  /* 星星闪烁动画 */
  @keyframes twinkle {
    0%, 100% { opacity: 0.3; }
    50% { opacity: 0.9; }
  }
  
  /* 流星划过动画 */
  @keyframes shootingStar {
    0% {
      transform: translateX(0) translateY(0) scale(1);
      opacity: 1;
      width: 2px;
      box-shadow: 0 0 6px 2px rgba(255, 255, 255, 0.8);
    }
    50% {
      width: 150px;
      opacity: 0.8;
    }
    100% {
      transform: translateX(-1000px) translateY(500px) scale(0.8);
      opacity: 0;
      width: 200px;
      box-shadow: 0 0 0 0 rgba(255, 255, 255, 0);
    }
  }
  
  /* 渐变背景移动动画 */
  @keyframes gradientShift {
    0%, 100% {
      background-position: 0% 0%, 100% 0%, 0% 100%;
    }
    33% {
      background-position: 30% 20%, 70% 30%, 20% 80%;
    }
    66% {
      background-position: 60% 40%, 40% 70%, 80% 20%;
    }
  }
  
  /* 背景文字动画 */
  @keyframes textGlow {
    0% {
      text-shadow: 0 0 5px rgba(255, 255, 255, 0.5), 0 0 10px rgba(255, 255, 255, 0.3);
      color: rgba(255, 255, 255, 0.7);
    }
    100% {
      text-shadow: 0 0 15px rgba(255, 255, 255, 0.8), 0 0 30px rgba(102, 126, 234, 0.6);
      color: rgba(255, 255, 255, 0.9);
    }
  }
  
  /* 文字浮动动画 */
  @keyframes floatText {
    0%, 100% {
      transform: translateY(0) translateX(0);
    }
    25% {
      transform: translateY(-10px) translateX(5px);
    }
    50% {
      transform: translateY(0) translateX(10px);
    }
    75% {
      transform: translateY(10px) translateX(5px);
    }
  }
  
  /* 背景文字样式 */
  .background-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 12rem;
    font-weight: 900;
    color: rgba(255, 255, 255, 0.05);
    pointer-events: none;
    z-index: -1;
    user-select: none;
    white-space: nowrap;
  }

/* 导航栏样式 - 改为不固定的普通导航栏 */
.navbar {
  background: transparent;
  padding: 1rem 0;
  z-index: 1000;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1.5rem;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
}

/* 新导航容器 - 横向排列所有项 */
.nav-container {
  display: flex;
  align-items: center;
  width: 100%;
  gap: 1.5rem;
}

/* 通用导航项样式 - 美化 */
.nav-item {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  padding: 0.6rem 1rem;
  border-radius: 8px;
  transition: all 0.3s ease;
  font-weight: 500;
  background: rgba(255, 255, 255, 0.05);
  display: flex;
  align-items: center;
  border: 1px solid rgba(255, 255, 255, 0.1);
  transform-style: preserve-3d;
  perspective: 1000px;
}

/* 导航项悬停效果 - 美化 */
.nav-item:hover {
  background: rgba(255, 255, 255, 0.1);
  transform: translateY(-2px) scale(1.02);
  color: rgba(255, 255, 255, 1);
  box-shadow: 0 2px 15px rgba(102, 126, 234, 0.3);
  border-color: rgba(102, 126, 234, 0.5);
}

/* 导航占位元素 - 用于将右侧元素推到最右边 */
.nav-spacer {
  flex-grow: 1;
}

/* 标题项特殊样式 */
.title-item {
  font-size: 1.8rem;
  font-weight: 800;
  color: rgba(255, 255, 255, 0.9);
  background: rgba(255, 255, 255, 0.08);
  padding: 0.6rem 1.2rem;
  text-shadow: 0 0 10px rgba(102, 126, 234, 0.6), 0 0 20px rgba(118, 75, 162, 0.4);
}

.title-item:hover {
  transform: scale(1.05);
  text-shadow: 0 0 15px rgba(102, 126, 234, 0.8), 0 0 30px rgba(118, 75, 162, 0.6);
}

/* VIP项特殊样式 */
.vip-item {
  background: linear-gradient(135deg, rgba(255, 159, 64, 0.2), rgba(254, 202, 87, 0.2));
  color: rgba(254, 202, 87, 0.9);
  border: 1px solid rgba(254, 202, 87, 0.3);
  font-weight: 700;
}

.vip-item:hover {
  background: linear-gradient(135deg, rgba(254, 202, 87, 0.3), rgba(255, 159, 64, 0.3));
  color: rgba(254, 202, 87, 1);
  box-shadow: 0 4px 15px rgba(255, 159, 64, 0.3);
}

/* 应用标题样式 - 融入背景效果 */
.app-title {
  font-size: 1.8rem;
  font-weight: 800;
}

.title-link {
  color: rgba(255, 255, 255, 0.9);
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  transition: all 0.3s ease;
  text-shadow: 0 0 10px rgba(102, 126, 234, 0.6), 0 0 20px rgba(118, 75, 162, 0.4);
}

.title-link:hover {
  transform: scale(1.05);
  color: rgba(255, 255, 255, 1);
  text-shadow: 0 0 15px rgba(102, 126, 234, 0.8), 0 0 30px rgba(118, 75, 162, 0.6);
}

/* 用户信息样式 - 美化 */
.user-info {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.3rem 0.8rem;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  transition: all 0.3s ease;
}

/* 用户信息悬停效果 - 美化 */
.user-info:hover {
  transform: translateY(-2px) scale(1.02);
  background: rgba(255, 255, 255, 0.1);
  box-shadow: 0 2px 15px rgba(102, 126, 234, 0.3);
  border-color: rgba(102, 126, 234, 0.5);
}

/* 欢迎文本样式 */
.welcome-text {
  color: rgba(255, 255, 255, 0.9);
  font-weight: 500;
  font-size: 1rem;
}

/* 登出按钮样式 */
.logout-button {
  padding: 0.4rem 0.8rem;
  background: rgba(255, 77, 79, 0.2);
  color: rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(255, 77, 79, 0.3);
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  transition: all 0.3s ease;
}

.logout-button:hover {
  background: rgba(255, 77, 79, 0.3);
  color: rgba(255, 255, 255, 1);
  transform: translateY(-1px);
}

/* 认证按钮容器 - 美化 */
.auth-buttons {
  display: flex;
  gap: 0.6rem;
  padding: 0.3rem;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  transition: all 0.3s ease;
}

/* 认证按钮容器悬停效果 - 美化 */
.auth-buttons:hover {
  transform: translateY(-2px) scale(1.02);
  background: rgba(255, 255, 255, 0.1);
  box-shadow: 0 2px 15px rgba(102, 126, 234, 0.3);
  border-color: rgba(102, 126, 234, 0.5);
}

/* 认证按钮样式 */
.auth-button {
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 500;
  text-decoration: none;
  transition: all 0.3s ease;
  border: 1px solid transparent;
  background: rgba(255, 255, 255, 0.1);
  color: rgba(255, 255, 255, 0.8);
}

.auth-button:first-child {
  background: rgba(99, 102, 241, 0.2);
  border: 1px solid rgba(99, 102, 241, 0.3);
}

.auth-button:last-child {
  background: rgba(66, 185, 131, 0.2);
  border: 1px solid rgba(66, 185, 131, 0.3);
}

.auth-button:hover {
  transform: translateY(-2px);
  color: rgba(255, 255, 255, 1);
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.2);
}

/* 导航链接样式 - 美化 */
.nav-links, .main-nav-links {
  display: flex;
  gap: 1.5rem;
}

.nav-link {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  font-size: 1rem;
  font-weight: 500;
  padding: 0.5rem 0.8rem;
  border-radius: 8px;
  transition: all 0.3s ease;
  position: relative;
  text-shadow: 0 0 5px rgba(102, 126, 234, 0.3);
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.nav-link:hover {
  background: rgba(255, 255, 255, 0.1);
  transform: translateY(-2px);
  color: rgba(255, 255, 255, 0.95);
  text-shadow: 0 0 10px rgba(102, 126, 234, 0.5);
}

.nav-link.active {
  background: rgba(255, 255, 255, 0.15);
  color: rgba(255, 255, 255, 1);
  box-shadow: 0 2px 10px rgba(102, 126, 234, 0.2);
  text-shadow: 0 0 10px rgba(102, 126, 234, 0.6);
  font-weight: 600;
}

.nav-link::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 2px;
  background-color: rgba(102, 126, 234, 0.7);
  transition: all 0.3s ease;
}

.nav-link:hover::after {
  width: 80%;
  left: 10%;
}

.nav-link.active::after {
  width: 80%;
  left: 10%;
  background-color: #42b983;
}

/* VIP链接特殊样式 - 融入背景 */
.vip-link {
  background: linear-gradient(135deg, rgba(255, 159, 64, 0.2), rgba(254, 202, 87, 0.2));
  color: rgba(254, 202, 87, 0.9) !important;
  padding: 0.5rem 1rem;
  border-radius: 25px;
  font-weight: 700;
  text-shadow: 0 0 10px rgba(255, 159, 64, 0.5);
  border: 1px solid rgba(254, 202, 87, 0.3);
}

.vip-link:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 15px rgba(255, 159, 64, 0.3);
  background: linear-gradient(135deg, rgba(254, 202, 87, 0.3), rgba(255, 159, 64, 0.3));
  color: rgba(254, 202, 87, 1) !important;
}

.vip-link::after {
  display: none;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  /* 主内容区域样式 - 调整padding */
.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
  min-height: calc(100vh - 100px);
}

.container {
    flex-direction: column;
    gap: 1rem;
    padding: 0 1rem;
  }
  
  .nav-content {
    flex-direction: column;
    gap: 1rem;
    width: 100%;
    text-align: center;
  }
  
  .app-title {
    font-size: 1.3rem;
  }
  
  .user-info {
    justify-content: center;
    width: 100%;
  }
  
  .auth-buttons {
    justify-content: center;
    width: 100%;
  }
  
  .nav-links {
    justify-content: center;
    flex-wrap: wrap;
    width: 100%;
    gap: 0.8rem;
  }
  
  /* 响应式主内容 */
  .main-content {
    padding: 1.5rem 1rem;
    min-height: calc(100vh - 120px);
  }
}

@media (max-width: 768px) {
  /* 进一步响应式调整 */
  .main-content {
    padding: 1rem 0.8rem;
  }
  
  .background-text {
    font-size: 8rem;
  }
}

@media (max-width: 768px) {
  .navbar {
    padding: 1rem 0;
  }
  
  .nav-links {
    gap: 0.8rem;
  }
  
  .nav-link {
    font-size: 0.9rem;
    padding: 0.4rem 0.6rem;
  }
  
  .welcome-text {
    font-size: 0.9rem;
  }
  
  .logout-button {
    padding: 0.4rem 1rem;
    font-size: 0.8rem;
  }
}
</style>
